package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.BaseOutStoreEntity;
import com.kaili.basic.pojo.vo.BaseOutStoreDetailVO;
import com.kaili.basic.pojo.vo.BaseOutStoreExportVO;
import com.kaili.basic.service.*;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Transactional
@Api(tags = "出库管理", description = "出库管理")
public class BaseOutStoreController {
    @Autowired
    private BaseOutStoreService  baseOutStoreService;

    @GetMapping("/outStore/code")
    @ApiOperation(value = "出库编号")
    public Long queryStoreOrderNo() {
        return baseOutStoreService.queryStoreOrderNo();
    }

    @GetMapping("/outStore")
    @ApiOperation(value = "分页查询出库")
    public Page<BaseOutStoreDetailVO> queryPageList(@ModelAttribute BaseOutStoreQueryDto queryDto){
        return baseOutStoreService.queryPageList(new Page<BaseOutStoreDetailVO>(queryDto.getPage(),queryDto.getPageSize()),queryDto);
    }

    @GetMapping("/outStore/detail/{orderNo}")
    @ApiOperation(value = "查询出库产品明细")
    List<BaseOutStoreDetailVO> queryDetailByOrderNo(@ApiParam("出库订单号") @PathVariable("orderNo") String orderNo){
        return baseOutStoreService.queryDetailByOrderNo(new HashMap<String,Object>(){{put("orderNo",orderNo);}});
    }

    @GetMapping("/outStore/edit/{id}")
    @ApiOperation(value = "编辑查询出库")
    public BaseOutStoreDetailVO queryOutStoreDetail(@PathVariable("id")  Long id){
        return baseOutStoreService.queryOutStoreDetail(new HashMap<String, Object>() {{
            put("id", id);
        }});
    }

    @PostMapping("/outStore")
    @ApiOperation(value = "新增出库单", notes = "新增出库单")
    public RestStatus addOutStore(@RequestBody BaseOutStoreAddDTO addDto){
        return baseOutStoreService.addOutStore(addDto);
    }

    @PostMapping("/outStore/byOutApply")
    @ApiOperation(value = "新增出库单(出库申请)", notes = "新增出库单")
    public RestStatus addOutApply(@RequestBody BaseOutApplyAddDTO applyAddDTO){
        try{
            List<BaseOutApplyAddDTO> addDTOList=new ArrayList<>();
            List<BaseOutApplyDetailAddDTO> list = applyAddDTO.getDetailList();
            for (BaseOutApplyDetailAddDTO addDTO:list) {
                BaseOutApplyAddDTO outApply=new BaseOutApplyAddDTO();
                BeanUtils.copyProperties(applyAddDTO,outApply);
                outApply.setStoreId(addDTO.getStoreId());
                List<BaseOutApplyDetailAddDTO> mList=new ArrayList<>();
                mList.add(addDTO);
                outApply.setDetailList(mList);
                addDTOList.add(outApply);
            }
            for (BaseOutApplyAddDTO dto:addDTOList) {
                baseOutStoreService.addOutApply(dto);
            }
            return CommonStatus.ADD_OK;
        }catch (Exception e){
            e.printStackTrace();
            return CommonStatus.STOCK_NOT_HAVE;
        }
    }

    @PutMapping("/outStore")
    @ApiOperation(value = "修改出库单", notes = "修改移库单")
    public RestStatus updateOutStore(@RequestBody BaseOutStoreEditDTO editDto){
        return baseOutStoreService.updateOutStore(editDto);
    }

    @PutMapping("/outStore/carrier")
    @ApiOperation(value = "设置出库物流运单", notes = "设置出库物流运单")
    public RestStatus updateOutStoreLogistics(@RequestBody BaseOutStoreLogisticsEditDto editDto){
        return baseOutStoreService.updateOutStoreLogistics(editDto);
    }

    @PutMapping("/outStore/audit")
    @ApiOperation(value = "审核出库单", notes = "审核出库单")
    public RestStatus auditOutStore(@RequestBody BaseOutStoreAuditDto auditDto){
        try{
            return baseOutStoreService.auditOutStore(auditDto);
        }catch (Exception e){
            e.printStackTrace();
            return CommonStatus.STOCK_NOT_HAVE;
        }
    }

    @DeleteMapping("/outStore/{id}")
    @ApiOperation(value = "删除移库")
    public RestStatus deleteOutStore(@ApiParam("订单号id")@PathVariable("id") Long id){
        BaseOutStoreEntity entity=new BaseOutStoreEntity();
        entity.setId(id);
        entity.setStatus("0");
        baseOutStoreService.updateById(entity);
        return CommonStatus.DELETE_OK;
    }

    @GetMapping("/outStore/export")
    @ApiOperation(value = "出库明细导出")
    public void outStoreExport(HttpServletResponse response,@ModelAttribute BaseOutStoreQueryDto queryDto) throws RestException {
        List<BaseOutStoreExportVO> list = baseOutStoreService.outStoreExport(queryDto);
        ExcelUtil.exportExcel(list,"出库管理","sheet1", BaseOutStoreExportVO.class, UUID.randomUUID().toString()+".xls",response);
    }
}


